﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Web.Core;
using Web.Prop;

namespace Web
{
    public partial class forgot : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                txEmail.Text = String.Empty;
            }
        }

        protected void btEmail_Click(object sender, EventArgs e)
        {
            try
            {
                ScriptManager scriptManager = ScriptManager.GetCurrent(this.Page);
                if (String.IsNullOrEmpty(txEmail.Text.Trim()))
                {
                    Alert.Message(this.Page, "กรุณากรอก Email");
                }
                else
                {
                    var o = Dao.USERDao.Init().Select(new USER()
                    {
                        EMAIL = txEmail.Text.Trim()
                    });
                    if (o.Count > 0)
                    {
                        String email = System.Configuration.ConfigurationManager.AppSettings["mailreply"];
                        String username = System.Configuration.ConfigurationManager.AppSettings["mailuser"];
                        String password = System.Configuration.ConfigurationManager.AppSettings["mailpass"];
                        String port = System.Configuration.ConfigurationManager.AppSettings["mailport"];
                        String host = System.Configuration.ConfigurationManager.AppSettings["mailhost"];

                        oProsonalMail q = new oProsonalMail();
                        q.Name = String.Format(@"{0}", username);
                        q.Email = email;

                        List<oProsonalMail> list = new List<oProsonalMail>();
                        oProsonalMail p = new oProsonalMail();
                        p.Name = String.Format(@"K.{0} {1}", o.ToList().First().FNAME, o.ToList().First().LNAME);
                        p.Email = o.ToList().First().EMAIL;
                        list.Add(p);

                        String boby = String.Format(@"
                            <div>
                                <table bgcolor='#ffffff' border='1' cellpadding='0' cellspacing='0' style='text-align: left;'>
                                    <tr>
                                        <td style='border: currentColor;' width='15'></td>
                                        <td style='border: currentColor; color: rgb(34, 34, 34); line-height: 18px; font-family: arial,sans-serif; 
                                        font-size: 13px;' valign='top' width='568'>
                                            สวัสดีค่ะคุณ " + String.Format("{0} {1} ", o.ToList().First().FNAME, o.ToList().First().LNAME) + @",
                                            <br />
                                            <br />
                                            รหัสผ่านสำหรับบัญชีของคุณ - <a href='mailto:" + o.ToList().First().EMAIL +
                                                            @"' target='_blank'>" + o.ToList().First().EMAIL + @"</a> 
                                            <br /><br /><br />
                                            <div style='margin-left: 10%; border-width: 1px; border-color: rgb(48, 121, 237); border-radius: 2px; 
                                        text-align: center; color: white; line-height: 27px; padding-right: 8px; padding-left: 8px; 
                                        font-family: arial,sans-serif; font-size: 11px; font-weight: bold; text-decoration: none; 
                                        display: inline-block; min-height: 40px; background-color: rgb(77, 144, 254);' >
                                                    <span style='color: white;'>username :&nbsp; " + o.ToList().First().UWD +
                                                            @"</span>
                                            <br />
                                                    <span style='color: white;'>password :&nbsp; " +
                                                            Core.Cipher.DecrytData(o.ToList().First().PWD) + @"</span>
                                            </div>
                                            <br />
                                            <br />
                                            ขอแสดงความนับถือ, <br />
                                            Asset Point Info Accounts team 
                                        </td>
                                        <td style='border: currentColor;' width='15'></td>
                                    </tr>
                                    <tr>
                                        <td height='15' style='border: currentColor;'></td>
                                    </tr>
                                    <tr>
                                        <td style='border: currentColor;' width='15'></td>
                                        <td style='border: currentColor; color: rgb(119, 119, 119); font-family: arial,sans-serif; font-size: 11px;' width='568'
                                            อีเมลนี้ไม่สามารถรับการตอบกลับ </td>
                                        <td style='border: currentColor;' width='15'></td>
                                    </tr>
                                </table>
                        ");

                        Core.Email.Gmail(mailFrom: q
                                         , mailTo: list
                                         , subject: "รหัสผ่านสำหรับบัญชีของคุณจาก Asset Point Info Accounts team"
                                         , message: boby
                                         , mail: email
                                         , port: port
                                         , host: host
                                         , username: username
                                         , password: password);
                        //
                        Dao.UserlogDao.Init().Add(new Userlog()
                        {
                            UID = o.ToList().First().UID
                            ,
                            LASTIP = Request.ServerVariables["REMOTE_ADDR"]
                            ,
                            UPDATE = DateTime.Now
                            ,
                            EVENT = Core.Events.RequestEmail.ToString()
                        });
                    }
                    else
                    {
                        Alert.Message(this.Page, "Email นี้ไม่มีอยู่ในระบบ");
                    }

                }
            }
            catch (Exception ex)
            {
                Dao.TransactionlogDao.Init().Add(new Transactionlog()
                {
                    UID = null
                    ,
                    EVENT = Core.Events.Error.ToString()
                    ,
                    LASTIP = Request.ServerVariables["REMOTE_ADDR"]
                    ,
                    UPDATE = DateTime.Now
                    ,
                    DESCRIPT = ex.Message
                });
            }

        }

        protected void btBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("singin.aspx", false);
        }
    }
}